package com.imooc.controller;

import com.imooc.BO.AddressBO;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gcq
 * @Create 2022-01-08
 */
@Api(value = "地址相关", tags = {"地址相关api接口"})
@RestController
@RequestMapping("address")
public class AddressController extends BaseController{

    @Autowired
    private AddressService addressService;

    /**
     * 用户确认订单后，可以针对订单做如下操作
     * 1、查询用户所有的地址收货列表
     * 2、新增收货地址
     * 3、删除收货地址
     * 4、修改收货地址
     * 5、设置默认地址
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户 Id 查询用户收货列表", notes = "根据用户 Id 查询用户收货列表", httpMethod = "POST")
    @PostMapping("/list")
    public IMOOCJSONResult info(@ApiParam(name = "userId",value = "用户Id", required = true)
                                  @RequestParam String userId)
    {
        if(StringUtils.isBlank(userId))
        {
            return IMOOCJSONResult.errorMsg("");
        }
        List<UserAddress> list = addressService.queryAll(userId);
        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
    @PostMapping("/add")
    public IMOOCJSONResult add(@RequestBody AddressBO addressBO)
    {
        IMOOCJSONResult checkRes = checkAddress(addressBO);
        if(checkRes.getStatus() != 200) {
            return IMOOCJSONResult.errorMsg("验证出错");
        }
        addressService.addNewUserAddress(addressBO);
        return IMOOCJSONResult.ok();
    }

    private IMOOCJSONResult checkAddress(AddressBO addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return IMOOCJSONResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
        }

        return IMOOCJSONResult.ok();
    }


    @ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")
    @PostMapping("/update")
    public IMOOCJSONResult update(@RequestBody AddressBO addressBO)
    {
        if(StringUtils.isBlank(addressBO.getAddressId())) {
            return IMOOCJSONResult.errorMsg("修改地址错误 addressId不能为空");
        }
        addressService.updateUserAddress(addressBO);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")
    @PostMapping("/delete")
    public IMOOCJSONResult del(@ApiParam(value = "用户Id")@RequestParam String userId,
                               @RequestParam String addressId)
    {
        if(StringUtils.isBlank(addressId) || StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("删除出错，addressId 或 userId 不能为空");
        }
        addressService.deleteUserAddress(userId, addressId);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "修改用戶默认地址", notes = "修改用戶默认地址", httpMethod = "POST")
    @PostMapping("/setDefalut")
    public IMOOCJSONResult setDefault(@ApiParam(value = "用户Id")@RequestParam String userId,
                               @RequestParam String addressId)
    {
        if(StringUtils.isBlank(addressId) || StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        addressService.updateUserAddressToBeDefault(userId, addressId);
        return IMOOCJSONResult.ok();
    }
}